#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Last Update:

'''农场操作, 相对我的农场
'''

__revision__ = '0.1'
__author__ = 'lxd'

class Helper():
    def __init__(self,log,dm, **kwargs):
        self.log = log
        self.dm = dm
        self.farmKind = kwargs.get('farmKind', None)

    def __isMyFarm(self):
        return self.dm.isColor(-437,77,'329df9')

    def __gotoMyFarm(self):
        if not self.__isMyFarm():
            self.dm.click(189,73)

    def __harvest(self):
        self.dm.click(181,560)
        self.dm.clickIfColor(16,337,'523b2a', wait=True)

    def doMyself(self):
        self.__gotoMyFarm()
        self.__water()
        self.__harvest()

    def __water(self):
        self.dm.click(-379,424)

    def getStealCrops(self):
        """得到需要的作物坐标
        """
        crops = []
        if self.farmKind['food']:
            items = self.dm.findMultiColors('6b5f15,-9|5|606410,9|6|383407', -461,17,512,595)
            if items: crops.extend(items)
        if self.farmKind['wood']:
            items = self.dm.findMultiColors('976a31,-9|5|fad8aa,4|3|714c1f', -461,17,512,595)
            if items: crops.extend(items)
        if self.farmKind['stone']:
            items = self.dm.findMultiColors('474c4f,-4|6|b6bbbe,6|5|6b7073', -461,17,512,595)
            if items: crops.extend(items)
        if self.farmKind['bronze']:
            items = self.dm.findMultiColors('697863,-8|4|8aa562,6|6|182a14', -461,17,512,595)
            if items: crops.extend(items)
        return crops

    def clickStealTool(self):
        self.dm.click(-114,559)

    def canSteal(self, index):
        """判断某人的地是否熟了
        """
        y = 185+index*24
        if self.dm.clickIfColor(486,y,'9f572d'):
            fname = self.__getFriendName(index)
            self.log.info(u'%s的作物已成熟' %fname)
            if self.__checkIfExiled():
                self.log.info(u'%s已流亡，请删除' %fname)
            else:
                return True
        return False

    def gotoFriend(self):
        self.dm.click(388,126)

    def gotoEmery(self):
        self.dm.click(447,129)

    def clickIfNeedWater(self, index):
        """根据序号判断
        """
        y = 185+index*24
        if self.dm.clickIfColor(465,y,'598CC2'):
            fname = self.__getFriendName(index)
            self.log.info(u'%s需要浇水' %fname)
            if self.__checkIfExiled():
                self.log.info(u'%s已流亡，请删除' %fname)
            else:
                self.__water()

    def clickNextPage(self):
        color = self.dm.getColor(458, 473)
        if color != 'afafaf' and color != 'c4c4c4':
            self.dm.click(458, 473)
            return True
        else:
            return False

    def selectFriendList(self):
        self.dm.clickIfColor(498,322,'FFFFFF', wait=True)

    def __checkIfExiled(self):
        if self.dm.isColor(-96,269,'FFFFFF'):
            self.dm.click(27,340)
            return True
        else:
            return False

    def __getFriendName(self,index):
        x0,y0,x1,y1 = 342,174,451,190
        left,right = x0,x1
        top = y0+24*index
        bottom = y1+24*index
        return self.dm.ocr(left,top,right,bottom,color = 'ffffff-000000|B3B3B3-000000')

